package com.sefer.dragonfly.client.core.consumer;

import java.io.Serializable;
import java.util.Collection;

import javax.management.MBeanOperationInfo;

import com.sefer.dragonfly.client.core.cache.CmCache;
import com.sefer.dragonfly.client.core.domain.BaseConsumer;
import com.sefer.dragonfly.client.core.domain.MonitorData;
import com.sefer.dragonfly.client.core.domain.mbean.annotation.CMbean;
import com.sefer.dragonfly.client.core.domain.mbean.annotation.CMbeanMethod;
import com.sefer.dragonfly.client.core.domain.scheduler.JobContext;
import com.sefer.dragonfly.client.tools.CmCacheTools;

/**
 * @author xiaofeng.zhouxf
 * 
 */
@CMbean
public class CmJvmConsumer extends BaseConsumer<String, String> {
	@CMbeanMethod(description = "CmJvmPlugin invoke", impact = MBeanOperationInfo.ACTION_INFO)
	public void execute() {
		JobContext<String, String> context = this.getJobContext();

		// 从内存缓冲中取出监控数据
		CmCache<Serializable> cache = CmCacheTools.getCache(this.getCacheId());
		if (cache == null) {
			return;
		}
		Collection<MonitorData<Serializable>> collection = cache.clear();
		if (collection == null || collection.size() == 0) {
			return;
		}

		for (MonitorData data : collection) {
			// 消费数据
			System.out.println(data.getValue());
		}
	}

	public void beforeExec() {
		JobContext<String, String> context = new JobContext<String, String>();
		this.setJobContext(context);
		// log
	}

	public void afterExec() throws Exception {
		// clear context
		JobContext<String, String> context;
		// log
	}
}
